package top.yxqz.servletconfig;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @author Mr.余
 * @date 2022/11/11
 * @package top.yxqz.servletconfig
 *
 */
public class ServletConfigDemo2  extends HttpServlet {
    private transient ServletConfig config;
    @Override
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String resp_encoding = config.getInitParameter("resp_encoding");
        String req_encoding = config.getInitParameter("req_encoding");
        request.setCharacterEncoding(req_encoding);
        response.setContentType(resp_encoding);

        //        得到init初始化的时候配置文件中的信息
//        参数是 配置文件中的key值
        String name = config.getInitParameter("001");
        System.out.println("name:"+name);

        PrintWriter writer = response.getWriter();
        writer.print("001号特工的名字："+name);


//        获取当前的servlet的别名（资源名称）
        String servletName = config.getServletName();
        writer.print("</br>Servlet的别名："+servletName);

        Enumeration<String> initParameterNames = config.getInitParameterNames();
//        String s = initParameterNames.nextElement();
//        writer.print("</br>initParameterNames："+s);
        while (initParameterNames.hasMoreElements()){
            String key = initParameterNames.nextElement();
            String value = config.getInitParameter(key);
            System.out.println("key:"+key+"\tvalue:"+value);
        }

    }
}
